package com.citywithincity.api.post.impl;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import com.citywithincity.api.post.ApiPost;
import com.citywithincity.api.post.ApiResponse;
import com.citywithincity.utils.HttpUtil;
import com.citywithincity.utils.JsonUtil;
import com.damai.action.json.impl.SignUtil;

public class MchApiPost implements ApiPost {
	
	private String baseUrl;
	private String mchId;
	private String key;
	
	public MchApiPost(String baseUrl, String mchId,String key) {
		this.mchId = mchId;
		this.key = key;
		this.baseUrl = baseUrl;
	}
	
	//"mch20170002","bfd013f77f3efc9b375aa63ddb3d08e1"
	@Override
	public ApiResponse post(String api, Map<String, Object> args, int crypt) throws IOException {
		TreeMap<String, Object> map = new TreeMap<String, Object>(args);
		map = SignUtil.prepareData(map,mchId,key);
		String content = HttpUtil.rawPost(baseUrl+api, JsonUtil.toJson(map));
		return new ApiResponse(JsonUtil.read(content));
	}

}
